/*
 * SalesMgr.java
 *
 * Created on April 4, 2007, 6:07 PM
 *
 * SalesMgr is responsiple for setting up the secure
 * communication link used for recieving SalesRecords
 * from the Vending machine, the SalesRecord Database,
 * and then adding SalesRecords to the DB as they are recieved
 *
 * TODO: after a record is successfully added to the
 * Local Site DB, a success status message should be
 * sent back to the vending machine (via secure comm link).
 */

package localSite;

import commpackage.*;
import java.util.Date;
import java.util.LinkedList;
import java.util.ListIterator;
import salesDB.*;

/**
 *
 * @author David Roberts
 */
public class SalesMgr
{
    //Sales Database
    private NoRacleSales salesDB;
    
    //Server variables
    private ConnectionType type;
    private CommFactory factory;
    private SalesMgrCB cb;
    private Comm com;
    private int sessionId;
    
    /** Creates a new instance of SalesMgr */
    public SalesMgr ()
    {
        System.out.println ("Creating Sales Manager");
        
        salesDB = new NoRacleSales ();
        
        //System.out.println ("Setting up Server.");
        type = new ConnectionType ();
        type.setHostname ("localhost");
        type.setPort (3000);
        type.setServer (true);
        type.setSecurity (false);
        factory = new CommFactory ();
        cb = new SalesMgrCB (this);
        
        //System.out.println ("Getting Comm instance");
        com = (Comm)factory.getCommInstance ();
        
        //System.out.println ("Starting New Server Session");
        sessionId = com.newSession (cb,type);
        com.connect (sessionId);
        com.getConnections (sessionId);
    }
    
    public void recordSale (SalesRecord rec)
    {
        salesDB.addRecord ( rec );
        System.out.println ("DB size: " + salesDB.databaseSize ());
        createReport ( new Date ());
//        SalesRecord dbRecord = salesDB.getRecord (rec.date, rec.transactionID);
//        if(dbRecord != null)
//        {
//            System.out.println ("TransID: " + dbRecord.transactionID +
//                    ", Total:" + dbRecord.transactionTotal);
//        }
//        else
//        {
//            System.out.println ("DB ERROR");
//        }
    }
    
    public void createReport (Date date)
    {
        LinkedList dateList = salesDB.getRecsByDate (date);
        ListIterator it = dateList.listIterator ();
        double total = 0.0;
        int numSales = 0;
        SalesRecord rec = null;
        
        System.out.println ("**Daily Sales Report for: " + date.toString () );
        
        while( it.hasNext () )
        {
            numSales++;
            rec = (SalesRecord) it.next ();
            System.out.println (numSales + ".  TransID: " + rec.transactionID + ", TrasactionTotal: " + rec.transactionTotal);
            total += rec.transactionTotal;            
        }
        System.out.println ("Number of Sales: " + numSales);
        System.out.println ("Total: " + total);
        System.out.println ("**End Report");
    }
}